package com.calligraphy.product.service.impl;

import com.calligraphy.bases.utils.JsonUtil;
import com.calligraphy.product.enums.ProductStatusEnum;
import com.calligraphy.product.enums.ResultEnum;
import com.calligraphy.product.exception.ProductException;
import com.calligraphy.product.model.ProductInfo;
import com.calligraphy.product.repository.ProductInfoRepository;
import com.calligraphy.product.service.ProductService;
import com.calligraphy.product.common.InventoryDTO;
import com.calligraphy.product.common.ProductInfoOutput;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author hy
 * @Date 2019/1/2 10:20
 **/
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductInfoRepository repository;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Override
    public ProductInfo findOne(String productId) {
        return repository.findById(productId).get();
    }

    @Override
    public ProductInfo save(ProductInfo productInfo) {
        return repository.save(productInfo);
    }

    @Override
    public List<ProductInfo> findUpAll() {
        return repository.findByProductStatus(ProductStatusEnum.UP.getCode());
    }

    @Override
    public Page<ProductInfo> findAll(Pageable pageable) {
        return repository.findAll(pageable);
    }

    @Override
    public List<ProductInfoOutput> findList(List<String> productIdList) {
        return repository.findByProductIdIn(productIdList).stream()
                .map(e -> {
                    ProductInfoOutput output = new ProductInfoOutput();
                    BeanUtils.copyProperties(e, output);
                    return output;
                })
                .collect(Collectors.toList());
    }

    @Override
    public void increaseStock(List<InventoryDTO> inventoryDTOList) {
        for (InventoryDTO inventoryDTO : inventoryDTOList){
            ProductInfo productInfo = repository.findById(inventoryDTO.getProductId()).get();
            if(productInfo ==null){
                throw new ProductException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            Integer result = productInfo.getProductStock()+inventoryDTO.getProductQuantity();
            productInfo.setProductStock(result);
            repository.save(productInfo);
        }
    }

    @Override
    @Transactional
    public void decreaseStock(List<InventoryDTO> inventoryDTOList) {
        List<ProductInfo> productInfoList = decreaseStockProcess(inventoryDTOList);

        //发送mq消息
        List<ProductInfoOutput> productInfoOutputList = productInfoList.stream().map(e -> {
            ProductInfoOutput output = new ProductInfoOutput();
            BeanUtils.copyProperties(e, output);
            return output;
        }).collect(Collectors.toList());

        amqpTemplate.convertAndSend("productInfo", JsonUtil.toJson(productInfoOutputList));

    }

    @Override
    public ProductInfo onSale(String productId) {
        ProductInfo productInfo = repository.findById(productId).get();
        if(productInfo==null){
            throw new ProductException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        if(productInfo.getProductStatusEnum()==ProductStatusEnum.UP){
            throw new ProductException(ResultEnum.PRODUCT_STATUS_ERROR);
        }
        productInfo.setProductStatus(ProductStatusEnum.UP.getCode());
        return repository.save(productInfo);
    }

    @Override
    public ProductInfo offSale(String productId) {
        ProductInfo productInfo=repository.findById(productId).get();
        if(productInfo==null){
            throw new ProductException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        if(productInfo.getProductStatusEnum()==ProductStatusEnum.DOWN){
            throw new ProductException(ResultEnum.PRODUCT_STATUS_ERROR);
        }
        productInfo.setProductStatus(ProductStatusEnum.DOWN.getCode());

        return repository.save(productInfo);
    }

    @Transactional
    public List<ProductInfo> decreaseStockProcess(List<InventoryDTO> decreaseStockInputList) {
        List<ProductInfo> productInfoList = new ArrayList<>();
        for (InventoryDTO decreaseStockInput: decreaseStockInputList) {
            ProductInfo productInfo = repository.findById(decreaseStockInput.getProductId()).get();
            //判断商品是否存在
            if (productInfo==null){
                throw new ProductException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            //库存是否足够
            Integer result = productInfo.getProductStock() - decreaseStockInput.getProductQuantity();
            if (result < 0) {
                throw new ProductException(ResultEnum.PRODUCT_STOCK_ERROR);
            }

            productInfo.setProductStock(result);
            repository.save(productInfo);
            productInfoList.add(productInfo);
        }
        return productInfoList;
    }
}
